/**
 * 
 */
package supermemo.input;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

/**
 * 
 * @author <a href='mailto:Yan-Qi@hotmail.com'>Yan Qi</a>
 * @version $Revision$
 * @latest $Id$
 */
public abstract class AbstractDataTablePanel extends JPanel
{
	protected JTable table = null;
	protected AbstractTableModel _data_model = null;
	protected SortFilterModel _sort_model = null;
	
	abstract protected void configure();
		
	/**
	 * Default constructor
	 * @param _table_data_model
	 */
	public AbstractDataTablePanel(AbstractTableModel data_model)
	{
		super();
		this._data_model = data_model;
		//
		setLayout(new BorderLayout());
		
//		_sort_model = new SortFilterModel(data_model);
		table = new JTable(_data_model);
		
		TableCellRenderer renderer = new EvenOddRenderer();
	    table.setDefaultRenderer(Object.class, renderer);
	    
		configure();
		
		JScrollPane scroll = new JScrollPane(table);
		scroll.setPreferredSize(new Dimension(600, 100));
	    this.add(scroll);
	}
	
	public void update(AbstractTableModel model)
	{
		table.setModel(model);
		
		configure();
	    
		update();
	}
	
	public void update()
	{
		table.updateUI();
	}

	/**
	 * @return the table
	 */
	public JTable getTable()
	{
		return table;
	}
}



/**
 * The code below can be found in the web-page : 
 * http://www.java2s.com/Code/Java/Swing-JFC/TableRendererSample.htm.
 * 
 * @author qyan
 */
class EvenOddRenderer implements TableCellRenderer {

	  public static final DefaultTableCellRenderer DEFAULT_RENDERER = new DefaultTableCellRenderer();

	  public Component getTableCellRendererComponent(JTable table, Object value,
	      boolean isSelected, boolean hasFocus, int row, int column) 
	  {
	    Component renderer = DEFAULT_RENDERER.getTableCellRendererComponent(
	        table, value, isSelected, hasFocus, row, column);
	    ((JLabel) renderer).setOpaque(true);
	    Color foreground, background;
	    if (isSelected) {
	      foreground = Color.blue;
	      background = Color.gray;
	    } else {
	      if (row % 2 == 0) {
	        foreground = Color.black;
	        background = Color.white;
	      } else {
	        foreground = Color.black;
	        background = Color.lightGray;
	      }
	    }
	    renderer.setForeground(foreground);
	    renderer.setBackground(background);
	    return renderer;
	  }

}